package xyz.msj.hrm.controller;


import org.apache.commons.io.FilenameUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import xyz.msj.hrm.util.FastDfsUtil;
import xyz.msj.hrm.util.AjaxResult;
import xyz.msj.hrm.util.encrypt.MD5;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Map;

/**
 * 文件服务
 */
@Controller
public class FileController {
    /**
     * 图片上传
     * @param file
     * @return
     */
    @PostMapping("/fastdfs/upload")
    @ResponseBody
    public AjaxResult upload(MultipartFile file) {
        try {
            //获取文件原始名
            String filename = file.getOriginalFilename();
            //获取文件后缀需要导入commios-io包）
            String extension = FilenameUtils.getExtension(filename);
            //开始上传，返回存储路径
            String path = FastDfsUtil.upload(file.getBytes(), extension);
            //返回路径
            return AjaxResult.me().setResultObj(path);
        } catch (IOException e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("上传失败！");
        }


    }

    /**
     * 图片删除
     * @param param //路径 /group1/M00/00/01/dhma1l9aQTCAakwLAAJYlTtgkLI904.jpg
     * @return
     */
    @PostMapping("/fastdfs/remove")
    @ResponseBody
    public AjaxResult remove(@RequestBody Map<String,String> param) {
        try {
            String path = param.get("path");
            // /group1/M00/00/01/dhma1l9aQTCAakwLAAJYlTtgkLI904.jpg
            //进行截取group1/M00/00/01/dhma1l9aQTCAakwLAAJYlTtgkLI904.jpg
            String group1 = path.substring(path.indexOf("/")+1);
            //获取组group1
            String subGroup1 = group1.substring(0, group1.indexOf("/"));
            //获取路径M00/00/01/dhma1l9aQTCAakwLAAJYlTtgkLI904.jpg
            String filePath = group1.substring(group1.indexOf("/")+1);
            //删除
            FastDfsUtil.delete(subGroup1, filePath);
            //删除成功
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("删除失败！");
        }
    }
    /**
     * 图片下载
     * @param param //路径 /group1/M00/00/01/dhma1l9aQTCAakwLAAJYlTtgkLI904.jpg
     * @return
     */
    @PostMapping("/fastdfs/download")
    public AjaxResult download(@RequestBody Map<String,String> param) {
        try {
            String path = param.get("path");
            // /group1/M00/00/01/dhma1l9aQTCAakwLAAJYlTtgkLI904.jpg
            //进行截取group1/M00/00/01/dhma1l9aQTCAakwLAAJYlTtgkLI904.jpg
            String group1 = path.substring(path.indexOf("/")+1);
            //获取组group1
            String subGroup1 = group1.substring(0, group1.indexOf("/"));
            //获取路径M00/00/01/dhma1l9aQTCAakwLAAJYlTtgkLI904.jpg
            String filePath = group1.substring(group1.indexOf("/")+1);
            //下载
            byte[] download = FastDfsUtil.download(subGroup1, filePath);
            //写出到指定目录
            FileOutputStream fileOutputStream = new FileOutputStream(new File("e:/"+ MD5.getRandomCode(3)+".jpg"));
            //进行下载
            fileOutputStream.write(download);
            //删除成功
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("下载失败！");
        }
    }

}
